package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author liyipeng
 * @date 2021-06-26 23:28
 * 上传图片，当用户上传图片的时候，利用fastdfs和feign调用将图片存到虚拟机中
 */
@RestController
public class FileController {

    @Value("${pic.url}")
    private String picPath;

    /**
     * 上传图片
     */
    @PostMapping("/upload")
    public String uploadFile(MultipartFile file) throws IOException {
        FastDFSFile fastDFSFile = new FastDFSFile();
        fastDFSFile.setContent(file.getBytes());
        //获取文件的扩展名字
        fastDFSFile.setExt(StringUtils.getFilenameExtension(file.getOriginalFilename()));
        String[] upload = FastDFSClient.upload(fastDFSFile);
        // http://192.168.211.132:8080/group1/M00/00/00/wKjThF-qeNyATiVHAAAl8vdCW2Y824.png
        String realPath = picPath+"/"+upload[0]+"/"+upload[1];
        //5.返回url给页面
        return realPath;
    }



}
